/**
 * 引导界面
 */

import UI_guide from "../../moduleui/guide/UI_guide";
import game from "../game";
import { UIBase } from "../gui/UIBase";
import { Fview, Gpath, Gtween, GtweenManager } from "../gui/UIDefine";
import { Logger } from "../log/logger";
import { GuideInfo } from "./GuideDefine";

export default class Guide extends UIBase<UI_guide, GuideInfo> {

    private _guideRange: Laya.Rectangle = new Laya.Rectangle();

    protected createView(): UI_guide {
        return UI_guide.createInstance();
    }

    protected initialize(): void {
        let view = this.view;
        view.setSize(game.width, game.height);
        view.m_bg.alpha = 0.6;
    }

    protected onOpen(): void {
        let view = this.view;
        view.m_mask.visible = false;
        view.m_from.visible = false;
        view.m_lbl.visible = false;

        Gtween.to(0, 0.6, 1).setTarget(view.m_bg, "alpha");

    }

    next(info: GuideInfo) {

        let view = this.view;

        view.m_lbl_say.visible = !!info.str;

        if (info.str) {
            view.m_lbl.ubbEnabled = !!info.ubb;
            view.m_lbl.text = info.str;
        }

        let targetView = this.ui.getOpenUI(info.view);
        if (!targetView) {
            this.close();
            Logger.logBusiness(info, "引导ui未打开");
            return;
        }

        let target: fgui.GObject = info.target instanceof Function ? info.target() : targetView[info.target];
        if (!target) {
            this.close();
            Logger.logBusiness(info, "引导ui未找到目标");
            return;
        }

        view.m_mask.visible = true;
        view.m_from.visible = true;

        let point = target.localToGlobal();
        let box2 = this._guideRange.setTo(point.x, point.y, target.width, target.height);

        let x = view.m_mask.x, y = view.m_mask.y, width = view.m_mask.width, height = view.m_mask.height;

        Gtween.kill(view.m_mask);
        Gtween.kill(view.m_from);
        Gtween.to4(width, height, x, y, box2.width, box2.height, box2.x, box2.y, 0.2).setTarget(view.m_mask, (w, h, x, y) => {
            view.m_mask.setSize(w, h);
            view.m_mask.setXY(x, y);
        });

        Gtween.to4(width, height, x, y, box2.width, box2.height, box2.x, box2.y, 0.2).setTarget(view.m_from, (w, h, x, y) => {
            view.m_from.setSize(w + 10, h + 10);
            view.m_from.setXY(x - 5, y - 5);
        });


        // view.m_mask.setSize(target.width, target.height);
        // view.m_mask.setXY(point.x, point.y);

        // view.m_from.setSize(target.width + 12, target.height + 12);
        // view.m_from.setXY(point.x - 6, point.y - 6);
    }

    private initBox(start: Fview, end: Fview) {


    }
}